/*
 Copyright (c) 2000-2001, Neil Rhodes and Julie McKeehan
   neil@pobox.com
 All rights reserved.

From the book "Palm Programming, the Developer's Guide, 2nd edition"
by O'Reilly.

Permission granted to use this file however you see fit.
*/

#define DO_NOT_ALLOW_ACCESS_TO_INTERNALS_OF_STRUCTS
#include <BuildDefines.h>
#ifdef DEBUG_BUILD
#define ERROR_CHECK_LEVEL ERROR_CHECK_FULL
#endif
#include <PalmOS.h>
#include "ResourceDefines.h"
#include "MainForm.h"


static 
void MainFormInit(FormPtr form)
{
#pragma unused(form)
  // warning-- don't do any drawing in this routine.
  // Also, don't call FrmSetFocus from here (it must be called *after*
  // FrmDrawForm)
}

static 
void MainFormDeinit(FormPtr form)
{
#pragma unused(form)
}


Boolean MainFormHandleEvent(EventPtr event)
{
  Boolean handled = false;
  FormPtr form;
  UInt32 a, b, c;
  Boolean color;
  a = 480;
  b = 320;
  c = 16;
  color = true;

  switch (event->eType) 
  {
  case frmOpenEvent:
    form = FrmGetActiveForm();
    MainFormInit(form);
 
    // Window Screen Size WIDTH, HEIGHT, DEPTH, COLOR
   WinScreenMode(winScreenModeSet, &a, &b, &c, &color);

   // We MUST have a custom policy to change Area/Trigger states
   FrmSetDIAPolicyAttr(form, frmDIAPolicyCustom);

   // Set area input to open. Otherwise Closed.
   PINSetInputAreaState(pinInputAreaOpen);

   // Disable the DIA trigger. Otherwise Enabled.
   PINSetInputTriggerState(pinInputTriggerEnabled);

   // Correct the window size when Input Area State is changed
   WinSetConstraintsSize(FrmGetWindowHandle(form), 320, 320, 320, 320, 
   450, 450);
   
   // Show Status Bar, also can be changed to Hide.
   StatShow();

   // Disable Orientation Trigger (or 0 = disable , 1 = enable)
   SysSetOrientationTriggerState(sysOrientationTriggerDisabled);
   // sysSetOrientationTriggerState(0) 

   FrmDrawForm(form);

    // here's where you'd add a call to FrmSetFocus
    handled = true;
    break;      
      
  case ctlSelectEvent:
  switch (event->data.ctlSelect.controlID) {
    case MainBeepButton:
      SndPlaySystemSound(sndWarning);
      handled = true;
      break;
        
    case MainGotoSecondFormButton:
      FrmGotoForm(SecondForm);
      handled = true;
      break;
    }
    break;

  case frmCloseEvent:
    MainFormDeinit(FrmGetActiveForm());
    handled = false;
    break;

  default:
    break;
  }  
  return handled;
}

